package com.singzi.leetcode.N500;

import com.sun.java_cup.internal.runtime.Scanner;

import java.util.Arrays;
import java.util.Objects;

public class T0468 {
    public static void main(String[] args) {
        Solution0468 solution0468 = new Solution0468();
        System.out.println(solution0468.validIPAddress("256.256.256.256"));
    }
}
class Solution0468 {
    public String validIPAddress(String queryIP) {
        if(queryIP.equals("")) return "\"Neither\"";
        String []ips = queryIP.split("\\.");
//        String []ip6s = queryIP.split(":");
        if(ips.length == 4 && isIPV4(ips)) return "IPv4";
        else if(ips.length == 8 && isIPV6(ips)) return "IPv6";
        else return "Neither";

    }

    private boolean isIPV6(String []ips) {
        for(String ip:ips){
            if(ip.length() > 3) return false;
            int  a;
            try {
                a = Integer.parseInt(ip);  //异常点
                System.out.println(a);
            } catch (Exception e) {
                return false;
            }
            if(a > 255 || a < 0) return false;
            for(int i = 0;i < ip.length();i++){
                if(ip.charAt(i) == '0' ) return false;
                if(ip.charAt(i) > '0') {
                    break;
                }
            }
        }
        return true;
    }

    private boolean isIPV4(String []ips) {

        for(String ip:ips){
            if(ip.length() > 4) return false;
            for(int i = 0;i < ip.length();i++){
                char c = ip.charAt(i);
                boolean f = c >= '0'&& c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F';
                if(!f) return false;
//                if(c > '9' && c < '0' &&)
            }
        }

        return true;
    }
}
